/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.maternity.servlets;

import br.com.maternity.classes.dao.PacienteDAO;
import br.com.maternity.classes.entity.Paciente;
import br.com.maternity.componentes.grid.PersonalGrid;
import br.com.maternity.componentes.grid.PersonalGridColumn;
import br.com.maternity.componentes.grid.PersonalGridStyle;
import br.com.maternity.util.Parser;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 *
 * @author Gattini
 */
@WebServlet(name = "listagemPacientes", urlPatterns = {"/listagemPacientes"})
public class listagemPacientes extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();


        ArrayList<PersonalGridColumn> cols = new ArrayList<PersonalGridColumn>();
        PersonalGridColumn c = new PersonalGridColumn("Prontuario", "getProntuario", "");
        c.setRedirectLink(request.getContextPath() + "/acoes/novoAtendimento.jsp?prontuario=@");
        ArrayList<String> qsf = new ArrayList<String>();
        qsf.add("getProntuario");
        c.setQueryStringFields(qsf);
        cols.add(c);

        c = new PersonalGridColumn("CPF", "getCpf", "");
        c.setWidth(15);
        c.setRedirectLink(request.getContextPath() + "/acoes/novoAtendimento.jsp?prontuario=@");
        c.setQueryStringFields(qsf);
        cols.add(c);

        c = new PersonalGridColumn("RG", "getRg", "");
        c.setWidth(15);
        c.setRedirectLink(request.getContextPath() + "/acoes/novoAtendimento.jsp?prontuario=@");
        c.setQueryStringFields(qsf);
        cols.add(c);

        c = new PersonalGridColumn("Nome", "getNome", "");
        c.setWidth(60);
        c.setRedirectLink(request.getContextPath() + "/acoes/novoAtendimento.jsp?prontuario=@");
        c.setQueryStringFields(qsf);
        cols.add(c);

        PersonalGridStyle rs = new PersonalGridStyle();

        PacienteDAO dao = new PacienteDAO();

        String rg = Parser.parseString(request.getParameter("txtRG"));
        String nome = Parser.parseString(request.getParameter("txtNome"));

        List<Paciente> source = dao.selectByRgOrNome(rg, nome + "%");

        PersonalGrid g = new PersonalGrid("paciente", "paciente", new ArrayList<Object>(source), cols, rs);

        try {
            out.write("0|<h1>Resultados da pesquisa</h1>" + g.render(null));
        } catch (Exception ex) {
            out.write("0|Ocorreu um erro ao realizar a busca dos pacientes no banco de dados");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
